import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jtsell_app/config/app_config.dart';
import 'package:jtsell_app/entity/cart_item_entity.dart';

class CartItemWidget extends StatelessWidget {
  final CartItemEntity data;
  final int index;
  final Function(int i) switchChaned;
  final Function(int i) addCount;
  final Function(int i) downCount;

  CartItemWidget(this.data, {this.switchChaned, this.index, this.addCount, this.downCount});

  @override
  Widget build(BuildContext context) {
    ///
    /// 列表项
    ///
    return Container(
      padding: EdgeInsets.only(
          top: ScreenUtil().setHeight(25),
          bottom: ScreenUtil().setHeight(15),
          right: ScreenUtil().setHeight(20),
          left: ScreenUtil().setHeight(25)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          ///
          /// 是否选中
          ///
          InkWell(
              onTap: () => switchChaned(index),
              child: Icon(
                data.isSelected ? Icons.check_circle_outline : Icons.radio_button_unchecked,
                color: AppConfig.THEME_COLOR,
              )),

          ///
          /// 商品图片
          ///
          Container(
            margin:
                EdgeInsets.only(left: ScreenUtil().setWidth(20), right: ScreenUtil().setWidth(15)),
            child: Image.network(data.imageUrl, fit: BoxFit.fill),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                ///
                /// 商品名称
                ///
                Text(data.productName),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    ///
                    /// 商品价格
                    ///
                    Text(
                      '￥${data.price}',
                      style: TextStyle(fontSize: 14, color: Colors.red),
                    ),

                    Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        ///
                        /// 商品-1
                        ///
                        GestureDetector(
                          onTap: () => data.buyCount > 1 && downCount(index),
                          child: Container(
                            width: ScreenUtil().setWidth(80),
                            height: ScreenUtil().setHeight(60),
                            decoration: BoxDecoration(border: _getRemoveBtBorder()),
                            child: Icon(Icons.remove, color: _getRemovebuttonColor()),
                          ),
                        ),

                        ///
                        /// 当前商品数量
                        ///
                        Container(
                            alignment: Alignment.center,
                            width: ScreenUtil().setWidth(80),
                            height: ScreenUtil().setHeight(60),
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: AppConfig.CART_ITEM_CHANGE_NUMBT_COLOR, width: 1)),
                            child: Text(
                              data.buyCount.toString(),
                              style: TextStyle(
                                  fontSize: 13,
                                  fontWeight: FontWeight.bold,
                                  color: AppConfig.CART_ITEM_CHANGE_NUMBT_COLOR),
                            )),

                        ///
                        /// 商品-1
                        ///
                        GestureDetector(
                          onTap: () => data.buyCount < data.buyLimit && addCount(index),
                          child: Container(
                            alignment: Alignment.center,
                            width: ScreenUtil().setWidth(80),
                            height: ScreenUtil().setHeight(60),
                            decoration: BoxDecoration(border: _getAddBtBorder()),
                            child: Icon(Icons.add, color: _getAddbuttonColor()),
                          ),
                        ),
                      ],
                    )
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  Color _getRemovebuttonColor() {
    return data.buyCount > 1 ? AppConfig.CART_ITEM_CHANGE_NUMBT_COLOR : AppConfig.CART_DISABLE_COLOR;
  }

  Border _getRemoveBtBorder() {
    return Border(
        bottom: BorderSide(width: 1, color: _getRemovebuttonColor()),
        top: BorderSide(width: 1, color: _getRemovebuttonColor()),
        left: BorderSide(width: 1, color: _getRemovebuttonColor()));
  }

  _getAddbuttonColor() {
    return data.buyCount >= data.buyLimit
        ? AppConfig.CART_DISABLE_COLOR
        : AppConfig.CART_ITEM_CHANGE_NUMBT_COLOR;
  }

  Border _getAddBtBorder() {
    return Border(
        bottom: BorderSide(width: 1, color: _getAddbuttonColor()),
        top: BorderSide(width: 1, color: _getAddbuttonColor()),
        right: BorderSide(width: 1, color: _getAddbuttonColor()));
  }
}
